Go + Gin で SPA 構成
Go のフレームワーク Gin を使ってSPAを作るメモ
Nginx で index.html へルーティングする手もあるけど、Twitter とかのクローラーは Javascript を読み込まないのでサーバー側である程度情報を埋め込んで上げる必要がある
code:main.go
func index(gc *gin.Context) {
// npm build が吐き出したHTMLを指定しとく
// TODO: テンプレートとして値を埋め込むことでOGP用のデータを出力したい
gc.HTML(http.StatusOK, "index.html", gin.H{
"title": "タイトル",
"description": "説明文",
})
}
func main () {
r := gin.Default()
r.LoadHTMLGlob("front/dist/*.html")
// なにがしらの API を提供
r.GET("/api/hoge", getHoge)
// SPA 用のHTMLを提供するため、ヒットしないものはすべてHTMLを返す
r.NoRoute(index)
}
OGPを埋める
code:index.html
<meta property="og:type" content="article"/>
<meta property="og:title" content="{{.title}}">
<meta property="og:description" content="{{.description}}">
Vue 用のテンプレートにこんな感じで書いておく
→ yarn run build でコンパイル
→ /dist/index.html にできたHTMLへサーバーで値を埋め込む